<?php
/**
 * IndexController - The default controller class
 *
 * @author
 * @version
 */
require_once 'Zend/Controller/Action.php';

class PlayerController extends App_Zend_Controller_Action_Shop
{

    
    public function shopbyplayerAction(){
    	$this->view->styleName = 'product-start';
    	$playerArray = CCache::getPlayerArray();
    	$this->view->playerArray = $playerArray;
    	$keywords = array();
    	foreach($playerArray as $playerInfo){
    		$keywords[]=$playerInfo['name'].' Soccer Jersey';
    	}
    	$this->view->webSiteTitle = 'Player Soccer Jersey, Free shipping ';
    	$this->view->webSiteDescription = 'Shop By Player at bestcheapjerseydeal! Free Shipping on all Super Player\'s Soccer Jerseys!';
    	$this->view->webSiteKeywords = implode($keywords,',');
    }
    
    public function playerdetailAction(){
    	$name = $this->getRequest()->getParam('name');
    	$realname = str_replace('-',' ',$name);	
    	$page = $this->_request->getParam('page',1);
    	if(!$page){$page=1;}
    	
    	$this->view->styleName = 'product-list';    	
    	$myCache=Zend_Registry::get('myCache');
		$cacheId=md5($realname.'-shop-by-player');
    	$result=$myCache->load($cacheId);
		if(!$result){
	    	$index=Zend_Search_Lucene::open(MY_ROOT."/indexdata/index");
			Zend_Search_Lucene_Search_QueryParser::setDefaultOperator(Zend_Search_Lucene_Search_QueryParser::B_OR);
			$query=Zend_Search_Lucene_Search_QueryParser::parse($realname,'utf-8');
			$hits=$index->find($query);
			$myHits=array();
			foreach($hits as $hit){
				$newHits=new stdClass();
				$newHits->PID=$hit->PID;
				$myHits[]=$newHits;
				unset($newHits);
			}
			$myCache->save($myHits,$cacheId);
		}else{
			$hits=$result;
		}
		
		$PIDS = array();
		foreach ($hits as $hit)
		{
			$PIDS[] = $hit->PID;
		}
		//L　SEARCH
		if(!empty($PIDS))
		{
			$itemCounts = count($PIDS);
		} else {
			$itemCounts = 0;
		}
		
		Zend_Loader::loadClass("CFilter");
    	$pageArray = CFilter::filterPage ($page, $itemCounts, 12);

		$itemArray = array();
		$myCache = Zend_Registry::get('myCache');
		$itemMap = CCache::getCombinlistItemMap();
		$offset = ($page-1)*12;
		for($i=$offset;$i<$offset+12;$i++){
			if($i<$itemCounts){
				array_push($itemArray,$itemMap[$PIDS[$i]]);
			}
		}
	
		$clubName = '';
		$nationName = '';
		$playerArray = CCache::getPlayerArray();
		foreach($playerArray as $playerInfo){
    		if($playerInfo['name'] == $realname){
    			$clubName = $playerInfo['club'];
    			$nationName = $playerInfo['national'];
    			$this->view->playerArray = array($playerInfo);
    			break;
    		}
    	}
    	
    	$attr = new CAttr();
    	$hasDesc = false;
    	$attrPlayer = $attr->exist($realname,3);
    	if($attrPlayer){
    		$hasDesc = true;
    		$this->view->attrPlayer = $attrPlayer;
    	}

		Zend_Loader::loadClass("CPage");
    	$pageString = CPage::divPagesPlayer( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/player-soccer-jersey/".$name); 		
		$this->view->assign ( "pageString", $pageString );
    	$this->view->assign ( "pageArray", $pageArray );
		$this->view->assign('itemArray',$itemArray);
		$this->view->assign('itemCounts',$itemCounts);
		$this->view->assign('page',$page);
		$this->view->assign('realname',$realname);
		$this->view->webSiteTitle = $realname.' Jersey, '.$realname.' Soccer Jerseys, '.$realname.' '.$clubName.' Soccer Jersey';
		$this->view->webSiteKeywords = $realname.' Home Jersey, '.$realname.' Away Jersey, '.$realname.' '.$clubName.' Jerseys, '.$realname.' '.$nationName.' Jerseys';
		if($hasDesc && $attrPlayer->attr_desc!=''){
			$this->view->webSiteDescription = CString::subStrs(str_replace("\n",".",$attrPlayer->attr_desc),350);
		}else{
			$this->view->webSiteDescription = 'Free Shipping on the '.$realname.' '.$clubName.' and '.$nationName.' Jerseys.';
		}
		
		
    }

    public function getproplayerAction()
    {
    	$this->_helper->viewRenderer->setNoRender();
        $this->getHelper ('layout')->disableLayout ();
		
		$playerArray = CCache::getPlayerArray();
		
		echo '<pre>';
		print_r($playerArray);
		echo '</pre>';
    }
    
    public function makeplayfileAction(){
    	$this->_helper->viewRenderer->setNoRender();
        $this->getHelper ('layout')->disableLayout ();
    
    
    	$content = file_get_contents("http://www.soccerpro.com/Favorite-Player-Jerseys/");
		
		//名字
		$pattern = '/<span class="category_page_indiv_name"><a[^>]+>([^<]+)<\/a><\/span>/';
		preg_match_all($pattern, $content, $playerNameMatches);
		
		$nameArray = array();
		foreach($playerNameMatches[1] as $value){
			$value = trim(str_replace('Jersey','',$value));
			$nameArray[] = $value;
		}
		
		//球星图片
		$pattern = '/<td width="69" valign="top">(<font size="1" face="Verdana">)?<img src="([^"]+)"/';
		preg_match_all($pattern, $content, $playerImgMatches);
		
		$imgArray = array();
		foreach($playerImgMatches[2] as $value){
			$value = trim($value);
			if(stristr($value,'http')){
				$imgUrl = $value;
			}else{
				$imgUrl='http://www.soccerpro.com/Favorite-Player-Jerseys/'.$value;
			}
			$imgdir = MY_ROOT.'/html/upload/player/';
			CFile::createFolder($imgdir);
			
			$fileTypeArray = explode ( ".", $imgUrl );
			$type = array_pop($fileTypeArray);
			$fileName = CString::randYzStr(5).'-'.time().'.'.$type;
			$path = $imgdir."/".$fileName;
			$isSuc =  CFile::openAndWrite($imgUrl,$path);
			if(!$isSuc){
				echo $imgUrl.'<br />';
			}
			$imgPath = 'html/upload/player/'.$fileName;
			$imgArray[] = $imgPath;
		}
		
		//描述
		$pattern = '/<span class="product_description">([^<]+)<\/span>/';
		preg_match_all($pattern, $content, $playerDescMatches);
		
		$birthArray = array();
		$clubArray = array();
		$nationalArray = array();
		for($i=0;$i<count($playerDescMatches[1]);$i++){
			if($i%3==0){
				$birthArray[] = trim($playerDescMatches[1][$i]);
			}else if($i%3==1){
				$clubArray[]=trim($playerDescMatches[1][$i]);
			}else if($i%3==2){
				$nationalArray[]=trim($playerDescMatches[1][$i]);
			}
		}
		
		$playerArray = array();
		for($i=0;$i<count($nameArray);$i++){
			$playerArray[$i]['name']=$nameArray[$i];
			$playerArray[$i]['img']=$imgArray[$i];
			$playerArray[$i]['birth']=$birthArray[$i];
			$playerArray[$i]['club']=$clubArray[$i];
			$playerArray[$i]['national']=$nationalArray[$i];
		}
		
		$attr = new CAttr();
		$playerAttr = $attr->getByType(3);
		
		foreach($playerAttr as $pl2Attr){
			$hasIn =false;
			foreach($playerArray as $pl1){
				if($pl1['name']==$pl2Attr->attr_name){
					$hasIn = true;
					break;
				}
			}
			if(!$hasIn){
				$parray = array('name'=>$pl2Attr->attr_name);
				$playerArray[] = $parray;
			}
		}
		
		usort($playerArray,array('PlayerController','sort'));

		
		$finalPlayerArray = array();
		foreach($playerArray as $playerInfo){
			$index=Zend_Search_Lucene::open(MY_ROOT."/indexdata/index");
			Zend_Search_Lucene_Search_QueryParser::setDefaultOperator(Zend_Search_Lucene_Search_QueryParser::B_OR);
			$query=Zend_Search_Lucene_Search_QueryParser::parse($playerInfo['name'],'utf-8');
			$hits=$index->find($query);
			if(count($hits)>0){
				$itemid = $hits[0]->PID;
				$item = new CItem();
				$itemDetailArray=$item->getDetail($itemid,'product');
				$fild = $itemDetailArray[0]['flid'];
				
				$productFl = new CFenlei('product');           
				$currentFenlei = $productFl->getoneFenlei($fild);
				
				$playerInfo['club'] = $currentFenlei[0]['en_flname'];
				$playerInfo['cateUrl'] = MY_URL.'/cd-'.$fild.'-0-1-'.$this->view->encodeName($currentFenlei[0]['en_flname']);
				
				$finalPlayerArray[]=$playerInfo;
			}
		}

		$playerContent = serialize($finalPlayerArray);
		file_put_contents(MY_ROOT.'/html/upload/nav/playercontent.txt',$playerContent);
    
    }
    
    public function sortplayerAction(){
    	$content = file_get_contents(MY_ROOT.'/html/upload/nav/playercontent.txt');
    	$playerArray = unserialize($content);
    	usort($playerArray,array('PlayerController','sort'));
    	$playerContent = serialize($playerArray);
		file_put_contents(MY_ROOT.'/html/upload/nav/playercontent.txt',$playerContent);
    }
    
    private static function sort($a, $b){ 
		$a1 = $a['name'][0];
		$b1 = $b['name'][0];
		return strcmp($a1,$b1);
	}

    public function postAction()
    {
        //$this->_helper->viewRenderer->setNoRender();
        //$this->getHelper ('layout')->disableLayout ();
    }

}
